#!/usr/bin/env python
#
# Copyright 2009 Liu, Xiaokang <4341322@gmail.com>
# Blog <http://www.ioio.name/>
#
import logging
from google.appengine.api import urlfetch
from google.appengine.ext import deferred

def handle_result(rpc):
    try:
        result = rpc.get_result()
        if result.status_code == 200:
            text = result.content
            logging.info(str(text))
    except urlfetch.DownloadError:
        logging.info('get_reslut() failure,kao ni mama de')
    # ... Do something with result...

# Use a helper function to define the scope of the callback.
def create_callback(rpc):
    return lambda: handle_result(rpc)


def main():
    urls = ['http://www.nuoqiu.com/static/5630/1216063.html',
            'http://open.chrome.360.cn/html/dev_samples.html']
    logging.info(str(urls))
    
    rpcs = []
    for url in urls:
        rpc = urlfetch.create_rpc(deadline=10)
        rpc.callback = create_callback(rpc)
        urlfetch.make_fetch_call(rpc, url)
        rpcs.append(rpc)
        #logging.info(str(rpc))


    # Finish all RPCs, and let callbacks process the results.
    for rpc in rpcs:
        rpc.wait()
  

if __name__ == '__main__':
    main()
